package com.atguigu.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.constant.MessageConstant;
import com.atguigu.entity.PageResult;
import com.atguigu.entity.QueryPageBean;
import com.atguigu.entity.Result;
import com.atguigu.pojo.Address;
import com.atguigu.service.AddressService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/address")
public class AddressController {

    @Reference
    private AddressService addressService;




    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        try {
            addressService.deleteById(id);
            return new Result(true,"删除成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除失败！");
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return addressService.findPage(queryPageBean);
    }


    @RequestMapping("/addAddress")
    public Result addAddress(@RequestBody Address address){
        try {
            addressService.save(address);
            return new Result(true, "添加成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加失败！");
        }
    }


    @RequestMapping("/findAllMaps")
    public Map<String,Object> findAllMaps(){
        List<Address> addressList= addressService.findAllMaps();
        List<Map<String,String>> gridnMaps = new ArrayList<Map<String,String>>();
        List<Map<String,String>> nameMaps = new ArrayList<Map<String,String>>();

        for (Address address : addressList) {
            String lng = address.getLng();
            String lat = address.getLat();
            HashMap<String, String> StringStringHashMap = new HashMap<>();
            StringStringHashMap.put("lng",lng);
            StringStringHashMap.put("lat",lat);
            gridnMaps.add(StringStringHashMap);


            HashMap<String,String> nameMap = new HashMap<>();
            String addressName = address.getAddressName();
            nameMap.put("addressName",addressName);
            nameMaps.add(nameMap);
        }
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("gridnMaps",gridnMaps);
        map.put("nameMaps",nameMaps);

        return map;
    }
}
